﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TravelWeb.Models;

namespace TravelWeb.Controllers
{ 
    public class DestinationController : Controller
    {
        private TravelWebDB db = new TravelWebDB();

        //
        // GET: /Destination/

        public ViewResult Index()
        {
            var destinations = db.Destinations.Include(d => d.Supplier);
            return View(destinations.ToList());
        }

        //
        // GET: /Destination/Details/5

        public ViewResult Details(int id)
        {
            Destination destination = db.Destinations.Find(id);
            return View(destination);
        }

        //
        // GET: /Destination/Create

        public ActionResult Create()
        {
            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName");
            return View();
        } 

        //
        // POST: /Destination/Create

        [HttpPost]
        public ActionResult Create(Destination destination)
        {
            if (ModelState.IsValid)
            {
                db.Destinations.Add(destination);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName", destination.SupplierID);
            return View(destination);
        }
        
        //
        // GET: /Destination/Edit/5
 
        public ActionResult Edit(int id)
        {
            Destination destination = db.Destinations.Find(id);
            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName", destination.SupplierID);
            return View(destination);
        }

        //
        // POST: /Destination/Edit/5

        [HttpPost]
        public ActionResult Edit(Destination destination)
        {
            if (ModelState.IsValid)
            {
                db.Entry(destination).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName", destination.SupplierID);
            return View(destination);
        }

        //
        // GET: /Destination/Delete/5
 
        public ActionResult Delete(int id)
        {
            Destination destination = db.Destinations.Find(id);
            return View(destination);
        }

        //
        // POST: /Destination/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Destination destination = db.Destinations.Find(id);
            db.Destinations.Remove(destination);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}